package com.xiaoyu.lanling.feature.friend.fragment

import `in`.srain.cube.views.list.CubeRecyclerViewSimpleAdapter
import `in`.srain.cube.views.list.PagedListDataModelWithRequestTag
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.xiaoyu.base.view.list.BaseListViewController
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.activity.base.BaseFragment
import com.xiaoyu.lanling.feature.friend.controller.FriendListViewController
import com.xiaoyu.lanling.feature.friend.data.FriendListData
import com.xiaoyu.lanling.feature.friend.model.FriendItem
import com.xiaoyu.lanling.feature.friend.viewholder.FriendViewHolder
import com.xiaoyu.lanling.view.OnShowFragment
import kotlinx.android.synthetic.main.fragment_friend.*

open class FriendListFragment : OnShowFragment() {

    private val adapter = CubeRecyclerViewSimpleAdapter<FriendItem>()

    private var listController: BaseListViewController<FriendItem, Any>? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return layoutInflater.inflate(R.layout.fragment_friend, container, false)
    }

    override fun onViewCreatedSafelyAfterAppFinishInit(view: View, savedInstanceState: Bundle?) {
        super.onViewCreatedSafelyAfterAppFinishInit(view, savedInstanceState)

        initAdapter()
        initData()
    }

    open fun getViewHolderClass(): Class<*> {
        return FriendViewHolder::class.java
    }

    open fun getListController(): BaseListViewController<FriendItem, Any>? {
        context?.let {
            val listData = FriendListData<FriendItem, Any>()
            return FriendListViewController.create(it, listData, null, recyclerview)
        }
        return null
    }

    private fun initAdapter() {
        adapter.setViewHolderClass(0, null, getViewHolderClass(), 20)
        recyclerview.adapter = adapter
    }

    private fun initData() {
        listController = getListController()
        listController?.loadFirstPageData()
    }
}